﻿using System.Collections.Generic;
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.DataStore;
using NTTDATA.BEENChina.ResultCertification.Logic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using NTTDATA.BEENChina.ResultCertification.Result;
using NTTDATA.BEENChina.ResultCertification.model;

namespace NTTDATA.BEENChina.ResultCertification.Test.Logic
{
    
    
    /// <summary>
    ///这是 CheckOrdersTest_ 的测试类，旨在
    ///包含所有 CheckOrdersTest_ 单元测试
    ///</summary>
    [TestClass()]
    public class CheckOrdersTest_
    {


        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        // 
        //编写测试时，还可使用以下特性:
        //
        //使用 ClassInitialize 在运行类中的第一个测试前先运行代码
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //使用 ClassCleanup 在运行完类中的所有测试后再运行代码
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //使用 TestInitialize 在运行每个测试前先运行代码
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //使用 TestCleanup 在运行完每个测试后运行代码
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///GetCompleteOrder 的测试
        ///</summary>
        [TestMethod()]
        public void GetCompleteOrderTest()
        {

            DataStore.DataTarget dataTarget = new DataTarget();
            dataTarget.RemoveOrders();

            CheckSetting checkCondition = new CheckSetting();
            
            checkCondition.RunTime = System.DateTime.Now;
            checkCondition.BeginDeliveryTime = DateTime.Parse("2013-08-06");
            checkCondition.EndDeliveryTime = DateTime.Parse("2013-08-07");


            CheckOrders target = new CheckOrders(checkCondition); // TODO: 初始化为适当的值
            target.EachOrderComplete -= new EventHandler<CheckOrdersEventArgs>(target_EachOrderComplete);
           // target.EachOrderComplete += new EventHandler<CheckOrdersEventArgs>(target_EachOrderComplete);
            target.GetCompleteOrder();

            //Assert.IsTrue();
        }

        void target_EachOrderComplete(object sender, CheckOrdersEventArgs e)
        {
            lock (new object())
            {
                
            
                System.Console.WriteLine("servercode is "+e.ServerCode);
                System.Console.WriteLine("order Count is " + e.OrderDatas.Count());

                CheckEntryResult checkEntryResult = new CheckEntryResult();
                checkEntryResult.CheckCategorys = new string[] { "24540700" };
                if (e.OrderDatas.Any())
                {
                    foreach (OrderData order in e.OrderDatas)
                    {
                        IEnumerable<ChildImage> childImages = checkEntryResult.GetEntryResult(order.ServerCode, order.OrderId);
                        new DataTarget().UpdateOrderStates(order.ServerCode, order.OrderId, EnumOrderState.录入数据分析);

                        System.Console.WriteLine(order.ServerCode + order.OrderId );
                        if (childImages.Any())
                        {

                           // new DataStore.DataTarget().InsertChildImage(childImages);
                            System.Console.WriteLine("categoryid = 24540700 ok"); 

                        }
                    }
                }
            }
        }
    }
}
